<?php
require('functions/page_load.php');

$content_nav = true;
$active_tab = 'details';

$results = ($_GET['new'] == 'true') ? '<span class="success_result" style="margin-bottom: 10px;">Article successfully created.</span>' : $results ;

if(isset($_GET['delete_category'])){

	$active_tab = 'categories';

		$sql = "DELETE FROM directory_to_cats WHERE id = '".mysql_real_escape_string($_GET['delete_category'])."'";
		$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

		$results = '<div class="success_result">Company deleted from category successfully!</div>';

}


if($_SERVER['REQUEST_METHOD'] == 'POST'){

	if(isset($_POST['add_category'])){
	
	$active_tab = 'categories';

		$sql = "INSERT INTO directory_to_cats SET category_id = '".mysql_real_escape_string($_POST['category_id'])."', directory_id = '".mysql_real_escape_string($_GET['id'])."'";
		$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

		$results = '<div class="success_result">Company added to category successfully!</div>';
	
	}

	if(isset($_POST['edit_details'])){

		// insert address
		
		$url = clean_file_name($_POST['company_name']);
		
			$address = $_POST['line_1'].",".$_POST['line_2'].",".$_POST['line_3'].",".$_POST['postcode'];
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, "http://maps.google.com/maps/geo?q=".urlencode($address)."&output=csv&oe=utf8&sensor=false&key=".$settings['google_api_key']);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				$output = curl_exec($ch);
				curl_close($ch);
			$outputArr = explode(",",$output);
			$latitude = $outputArr[2];
			$longitude = $outputArr[3];
		
		$sql = "UPDATE directory SET 
				company_name = '".mysql_real_escape_string($_POST['company_name'])."',
				url = '".mysql_real_escape_string($url)."',
				area_id = '".mysql_real_escape_string($_POST['area_id'])."', 
				line_1 = '".mysql_real_escape_string($_POST['line_1'])."', 
				line_2 = '".mysql_real_escape_string($_POST['line_2'])."', 
				line_3 = '".mysql_real_escape_string($_POST['line_3'])."', 
				postcode = '".mysql_real_escape_string($_POST['postcode'])."',
				latitude = '".$latitude."',
				longitude = '".$longitude."',
				telephone = '".mysql_real_escape_string($_POST['telephone'])."',
				website = '".mysql_real_escape_string($_POST['website'])."',
				email = '".mysql_real_escape_string($_POST['email'])."',
				linkback_address = '".mysql_real_escape_string($_POST['linkback_address'])."',
				description = '".mysql_real_escape_string($_POST['description'])."',
				nolinkback = '".mysql_real_escape_string($_POST['nolinkback'])."'
				WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
		$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

		$results = '<div class="success_result">Company updated successfully!</div>';
		
	}

}

$sql = "SELECT company_name, area_id, line_1, line_2, line_3, postcode, telephone, website, email, linkback_address, description, nolinkback FROM directory WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
$rs = mysql_fetch_assoc($query);

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Navigation</h2>

<ul>
<li><a href="content.php">Pages</a></li>
<li><a href="blog.php">Blog</a></li>
<li><a href="directory.php">Directory</a></li>
<li><a href="faqs.php">FAQs</a></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">Edit Company</h2>
	
	<div id="top_links">
	<a class="tab_link" rel="#categories" style="background-image: url(images/list.png); padding-left: 20px;">Categories</a>
	<a class="tab_link" rel="#details" style="background-image: url(images/page.png); padding-left: 20px;">Company details</a>
	<a href="directory.php" style="background-image: url(images/back.png); padding-left: 20px;">Back to Directory</a>
	</div>
	
	<?php echo $results; ?>
	
	<div id="details" class="tab_container"<?php echo ($active_tab == 'details') ? ' style="display: block;"' : ' style="display: none;"' ; ?>>
	
			<form method="post" action="">
			
			<input type="hidden" name="edit_details" value="true" />
			
			<!-- user details -->
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="company_name">Company name:</label></div>
			<div class="form_row_right"><input type="text" name="company_name" id="company_name" class="textbox" value="<?php echo $rs['company_name']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="description">Description:</label></div>
			<div class="form_row_right"><textarea name="description" id="description" rows="6" cols="30" class="textbox" style="width: 520px; height: 200px;"><?php echo $rs['description']; ?></textarea></div>
			</div>
			
			<!-- address details -->
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="line_1">Address:</label></div>
			<div class="form_row_right">
			<input type="text" name="line_1" id="line_1" class="textbox" value="<?php echo $rs['line_1']; ?>" />
			<input type="text" name="line_2" id="line_2" class="textbox" value="<?php echo $rs['line_2']; ?>" />
			<input type="text" name="line_3" id="line_3" class="textbox" value="<?php echo $rs['line_3']; ?>" />
			<select name="area_id" id="area_id" style="margin-top: 5px;" class="textbox clearLeft">
			<?php
			
			$sql = "SELECT id, area FROM areas ORDER BY area";
			$area_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($area_rs = mysql_fetch_assoc($area_query)){
			
			$selected = ($rs['area_id'] == $area_rs['id']) ? ' selected="selected"' : '' ;
			
			echo '<option value="'.$area_rs['id'].'"'.$selected.'>'.$area_rs['area'].'</option>';
			
			}
			
			?></select>
			<input type="text" name="postcode" id="postcode" class="textbox clearLeft" value="<?php echo $rs['postcode']; ?>" style="width: 80px;" />
			</div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="website">Website:</label></div>
			<div class="form_row_right"><input type="text" name="website" id="website" class="textbox" value="<?php echo $rs['website']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="telephone">Telephone:</label></div>
			<div class="form_row_right"><input type="text" name="telephone" id="telephone" class="textbox" value="<?php echo $rs['telephone']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="email">Email:</label></div>
			<div class="form_row_right"><input type="text" name="email" id="email" class="textbox" value="<?php echo $rs['email']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="linkback_address">Linkback address:</label></div>
			<div class="form_row_right"><input type="text" name="linkback_address" id="linkback_address" class="textbox" value="<?php echo $rs['website']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="nolinkback">No Linkback:</label></div>
			<div class="form_row_right"><input type="checkbox" name="nolinkback" id="nolinkback" value="1"<?php echo ($rs['nolinkback'] == '1') ? ' checked="checked"' : '' ; ?> /><span style="margin-left: 10px;">Tick this to override the linkback requirements.</span></div>
			</div>

			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
	</div>
			
	<div id="categories" class="tab_container"<?php echo ($active_tab == 'categories') ? ' style="display: block;"' : ' style="display: none;"' ; ?>>
			
	
	<table width="100%" cellpadding="0" cellspacing="0" class="cms_table">
		<thead><tr><th align="left">Category</th><th width="50">Delete</th></tr></thead>
		<tbody>
		<?php
		
		$sql = "SELECT directory_to_cats.id, category FROM directory_to_cats
				LEFT JOIN directory_cats ON directory_to_cats.category_id = directory_cats.id
				WHERE directory_id = ".mysql_real_escape_string($_GET['id'])."";
		$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
		$rows = mysql_num_rows($query);
		
		if($rows > 0){
		
			while($rs=mysql_fetch_assoc($query)){
			
			echo '<tr><td align="left" valign="top">'.$rs['category'].'</td><td valign="top"><a onclick="confirm_delete(\'?id='.$_GET['id'].'&amp;delete_category='.$rs['id'].'\',\'this company from this category\');" title="Delete"><img src="images/delete.png" alt="Delete" /></a></td></tr>';
			
			}
			
		}
		
		else{
		
		echo '<tr><td colspan="2">There are currently no categories for this company.</td></tr>';
		
		}
		
		?>
		
		</tbody></table>
		
			<form method="post" action="">
			
			<input type="hidden" name="add_category" value="true" />

			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="trade_id">Add category:</label></div>
			<div class="form_row_right"><select name="category_id" id="category_id">
			<?php
			
			$sql = "SELECT id, category FROM directory_cats ORDER BY category";
			$trades_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($trades_rs = mysql_fetch_assoc($trades_query)){
			
				echo '<option value="'.$trades_rs['id'].'">'.$trades_rs['category'].'</option>';
			
			}
			
			?>
			</select></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
			
			
	</div>
			

	</div>

</div>
	
<?php
	require('includes/footer.php');
?>